using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ECatalog.Controllers;
using ECatalog.Models.Models;
using ECatalog.Models.Organizations;

namespace ECatalog.Models.Kits
{
    public class KitRepository : IKitRepository
    {
        private readonly IOrganizationRepository organizationRepository;
        private readonly IModelRepository modelRepository;

        public KitRepository(IOrganizationRepository organizationRepository, IModelRepository modelRepository)
        {
            this.organizationRepository = organizationRepository;
            this.modelRepository = modelRepository;
        }

        public Kit AddKit(Kit kit)
        {
            using (var db = new DbContainer())
            {
                db.Kits.Add(kit);
                db.SaveChanges();
                return kit;
            }
        }

        public Kit[] GetAllKits()
        {
            using (var db = new DbContainer())
            {
                return db.Kits.Where(k => !k.IsDeleted).ToArray();
            }
        }

        public Kit[] GetAllKitsInOrganization(int organizationId)
        {
            using (var db = new DbContainer())
            {
                return db.Kits.Where(k => !k.IsDeleted && k.OrganizationId == organizationId).ToArray();
            }
        }

        public Kit[] GetAllKitsInOrganizationByModelId(int organizationId, int modelId)
        {
            using (var db = new DbContainer())
            {
                var kitIds = db.KitModels.Where(km => km.ModelId == modelId).Select(km=>km.KitId).ToArray();
                var kits = db.Kits.Include("KitModels").Where(k => !k.IsDeleted && k.OrganizationId == organizationId && kitIds.Contains(k.KitId)).ToArray();
                return kits;
            }
        }

        public Kit GetKitById(int id)
        {
            using (var db = new DbContainer())
            {
                return db.Kits.FirstOrDefault(k => k.KitId == id);
            }
        }

        public void RemoveKitById(int id)
        {
            using (var db = new DbContainer())
            {
                var model = db.Kits.FirstOrDefault(u => u.KitId == id);
                if (model != null)
                {
                    model.IsDeleted = true;
                    db.SaveChanges();
                }
            }
        }

        public void SaveKit(Kit kit)
        {
            using (var db = new DbContainer())
            {
                db.Entry(kit).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public KitViewModel GetViewModel(int id, int? organizationId)
        {
            using (var db = new DbContainer())
            {
                var kitModel = db.Kits.FirstOrDefault(u => u.KitId == id);
                if (kitModel != null)
                {                    
                    var kitModels = db.KitModels.Include("Model").Where(k => k.KitId == id).ToArray();
                    var models = modelRepository.GetAllModels();
                    if (organizationId != null)
                    {
                        models = models.Where(md => md.OrganizationId == organizationId).ToArray();
                        var shops = db.Shops.Where(sh => sh.OrganizationId == organizationId).ToArray();
                        var selectmodels = new List<Model>();
                        foreach (var model in models)
                        {
                            var f = kitModels.Any(km => km.ModelId == model.ModelId);
                            if (!f)
                            {
                                selectmodels.Add(model);
                            }
                        }

                        var kitViewModel = new KitViewModel
                            {
                                ImageUrl = kitModel.ImageUrl,
                                Description = kitModel.Description,
                                OrganizationId = kitModel.OrganizationId,
                                IsDeleted = kitModel.IsDeleted,
                                KitId = kitModel.KitId,
                                KitModels = kitModels,
                                Organization = kitModel.Organization,
                                Sale = kitModel.Sale,
                                SalePrice = kitModel.SalePrice,
                                Organizations = organizationRepository.All(),
                                Models = models,
                                SelectModels = selectmodels.ToArray(),
                                Shops = shops,
                                TypeImage = kitModel.TypeImage
                            };
                        return kitViewModel;
                    }
                }
                return new KitViewModel
                    {
                        Organizations = organizationRepository.All()
                    };
            }
        }

        public void AddKitModels(int[] ids, int kitId)
        {
            var kitModels = ids.Select(i => new KitModel
                {
                    ModelId = i,
                    KitId = kitId
                }).ToArray();

            using (var db = new DbContainer())
            {
                try
                {
                    foreach (var kitModel in kitModels)
                    {
                        db.KitModels.Add(kitModel);
                    }
                    db.SaveChanges();
                }
                catch (Exception)
                {
                }

            }
        }

        public void DeleteKitModel(int modelid, int kitId)
        {
            using (var db = new DbContainer())
            {
                try
                {
                    var km = db.KitModels.SingleOrDefault(k => k.ModelId == modelid && k.KitId == kitId);
                    if (km != null)
                    {
                        db.KitModels.Remove(km);
                        db.SaveChanges();
                    }
                }
                catch (Exception)
                {
                }

            }
        }
    }
}